package com.tanhua.app.controller;

import com.aliyun.oss.ClientException;
import com.tanhua.app.service.UserService;
import com.tanhua.domain.db.User;
import com.tanhua.domain.db.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;


    @GetMapping(name = "根据手机号查询用户",value = "/findByPhone")
    public ResponseEntity findByPhone(String phone){
        return userService.findByphone(phone);
    }


    @PostMapping(name = "保存用户",value = "/saveUser")
    public ResponseEntity saveUser(@RequestBody User user){
        return userService.saveUser(user);
    }

    @PostMapping(name = "发短信",value = "/login")
    public ResponseEntity login(@RequestBody Map<String,String> map)throws ClientException{
        String phone = map.get("phone");
        return userService.login(phone);
    }

    @PostMapping(name = "登录",value = "/loginVerification")
    public ResponseEntity loginVerification(@RequestBody Map<String,String> map)throws ClientException{

        //1 获取手机号和验证码
        String phone = map.get("phone");
        String verificationCode = map.get("verificationCode");
        return userService.loginVerification(phone,verificationCode);
    }
    @PostMapping(value = "/loginReginfo", name = "用户个人信息完善")//请求参数Authorization 令牌=token
    public ResponseEntity loginReginfo(@RequestBody UserInfo userInfo, @RequestHeader("Authorization") String token) throws ClientException {
        return userService.loginReginfo(userInfo,token);
    }

    @PostMapping(value = "/loginReginfo/head", name = "人脸识别/图片上传/修改个人信息")//请求参数Authorization 令牌=token
    public ResponseEntity loginReginfoimg(MultipartFile headPhoto, @RequestHeader("Authorization") String token) throws ClientException, IOException {
       return userService.loginReginfoimg(headPhoto,token);
    }

}
